<template>
  <el-select @change="changeChannel" :value="value" placeholder="请选择" clearable>
    <el-option v-for="item in channelOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
    <!-- label 选项文字  value 选项的值 当你选择某个选项后，该选项的值提供v-model -->
  </el-select>
</template>

<script>
export default {
  name: "my-channel",

  props: ["value"],
  data() {
    //默认选中
    return {
      // value:null,
      // 所有下拉选项
      channelOptions: []
    };
  },
  created() {
    this.getChannelOptions();
  },
  methods: {
    changeChannel(value) {
      // if (this.value === "") this.value = null;
      //通知父组件改值
      if(value === '') value = null
      this.$emit('input',value)
    },
    // 获取频道数据
    async getChannelOptions() {
      // 发请求获取频道数据
      const res = await this.$http.get("channels");
      // res = {data:{message:'',data:{channels:[// 频道数组 ]}}}
      // this.channelOptions = [{id,name}]  数据格式
      this.channelOptions = res.data.data.channels;
    }
  }
};
</script>
<style scoped>
</style>